package lib;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utils {

	public static Map parseRequestPath(String path) {
		Map ret = new HashMap<String, Object>();
		String controllerName = null;
		List<String> params = new ArrayList<String>();
		String[] strings = path.split("/");
		for (int i = 0; i < strings.length; i++) {
			if (i==0)
				controllerName = strings[i];
			else
				params.add(strings[i]);
		}
		if (controllerName!=null)
			ret.put("controllerName", controllerName);
		if (params.size()>0)
			ret.put("params", params);
		return ret;
	}
	
	public static Object getInstance(String className, ArrayList<Object> params) throws Exception {
		Object ret = null;
        Class classObj;
        try {
        	classObj = Class.forName(className);
        	if (params.size()==0) {
        		ret = classObj.newInstance();
        	} else {
            	ret = classObj.getConstructor().newInstance(params);
        	}
		} catch (Exception e) {
			throw e;
		}
        return ret;
	}
	
}
